package com.vogue.supplymall.entmanagement.controller;

import com.vogue.supplymall.entmanagement.domain.biz.LicenceShopSearchBiz;
import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.LicenceShopSearchInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * The type Licence shop search controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-16
 */
@RestController
public class LicenceShopSearchController extends BaseController {
    /**
     * The constant LOGGER_LICENCESHOPSEARCHCONTROLLER.
     */
    public static final Logger LOGGER_LICENCESHOPSEARCHCONTROLLER = LoggerFactory.getLogger(LicenceShopSearchController.class);
    @Resource
    private LicenceShopSearchBiz licenceShopSearchBiz;

    /**
     * Find current shop list response entity.
     * 店铺管理-当前店铺查询
     *
     * @param appver                the appver
     * @param licenceShopSearchInfo the licence shop search info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/entmanagement/store/current/list", method = RequestMethod.POST)
    public ResponseEntity<Object> findCurrentShopList(@PathVariable("appver") String appver,
                                                      @RequestBody LicenceShopSearchInfo licenceShopSearchInfo) {
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info(">>> find Current Shop List start ");
        if (null == licenceShopSearchInfo.getStart() ||
                null == licenceShopSearchInfo.getLength()) {
            throw new DataNotFindException("parameter error");
        }
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            licenceShopSearchInfo.setSyscodeid(info.getSyscodeid());
            licenceShopSearchInfo.setApplytype(1);
        } else {
            LOGGER_LICENCESHOPSEARCHCONTROLLER.info("获取登信息失败！");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(licenceShopSearchBiz.findCurrentShopList(licenceShopSearchInfo));
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info("find Current Shop List  end <<<");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Find renewal shop list response entity.
     * 店铺管理-续约店铺查询
     *
     * @param appver                the appver
     * @param licenceShopSearchInfo the licence shop search info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/entmanagement/store/renewal/list", method = RequestMethod.POST)
    public ResponseEntity<Object> findRenewalShopList(@PathVariable("appver") String appver,
                                                      @RequestBody LicenceShopSearchInfo licenceShopSearchInfo) {
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info(">>> find renewal Shop List start ");
        if (null == licenceShopSearchInfo.getStart() ||
                null == licenceShopSearchInfo.getLength()) {
            throw new DataNotFindException("parameter error");
        }
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            licenceShopSearchInfo.setSyscodeid(info.getSyscodeid());
            licenceShopSearchInfo.setApplytype(2);
        } else {
            LOGGER_LICENCESHOPSEARCHCONTROLLER.info("获取登信息失败！");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(licenceShopSearchBiz.findCurrentShopList(licenceShopSearchInfo));
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info("find renewal Shop List  end <<<");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Find warning response entity.
     * 店铺管理-警告数查询
     *
     * @param appver the appver
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/entmanagement/store/statistics/warning", method = RequestMethod.POST)
    public ResponseEntity<Object> findWarning(@PathVariable("appver") String appver) {
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info(">>> count warning start ");
        LicenceShopSearchInfo licenceShopSearchInfo = new LicenceShopSearchInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            licenceShopSearchInfo.setSyscodeid(info.getSyscodeid());
        } else {
            LOGGER_LICENCESHOPSEARCHCONTROLLER.info("获取登信息失败！");
        }
        DealStatus.kacha();
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(licenceShopSearchBiz.findWarning(licenceShopSearchInfo));
        LOGGER_LICENCESHOPSEARCHCONTROLLER.info("count warning end <<<");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
